﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skynet.BusinessEntities;

namespace Skynet.BusinessLogic.DataAccess
{
	public class KindSQLRepository : IKindRepository
	{
		public void Add(Kind item)
		{
			string str = System.Configuration.ConfigurationManager.ConnectionStrings["SkynetLibrary"].ConnectionString;
			System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(str);            
			System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("insert into kinds values (@id, @title, @description)", con);

			cmd.CommandType = System.Data.CommandType.Text;
			cmd.Parameters.AddWithValue("@id", item.Id);
			cmd.Parameters.AddWithValue("@title", item.Title);
			cmd.Parameters.AddWithValue("@description", item.Description); 
			try
			{
				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				//
			}
			finally
			{
				con.Close();
			}                 
		}

		public void Add(IEnumerable<Kind> items)
		{
			string str = System.Configuration.ConfigurationManager.ConnectionStrings["SkynetLibrary"].ConnectionString;
			System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(str);
			System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("insert into kinds values (@id, @title, @description)", con);
			cmd.CommandType = System.Data.CommandType.Text;
			cmd.Parameters.Add("@id",  System.Data.SqlDbType.UniqueIdentifier);
			cmd.Parameters.Add("@title", System.Data.SqlDbType.VarChar,  50);
			cmd.Parameters.Add("@description",  System.Data.SqlDbType.VarChar,  250);
			System.Data.SqlClient.SqlTransaction  tran =  null;
			try
			{
				con.Open();
				tran =  con.BeginTransaction( System.Data.IsolationLevel.ReadCommitted);                  
				cmd.Transaction = tran; 
				foreach (var item in items)
				{
					cmd.Parameters["@id"].Value = item.Id;  
					cmd.Parameters["@title"].Value =  item.Title; 
					cmd.Parameters["@description"].Value =  item.Description; 
					cmd.ExecuteNonQuery();    		 
				}                
				tran.Commit();
			}
			catch (Exception ex)
			{
				if ( tran != null) 
					tran.Rollback();  
			}
			finally
			{
				con.Close();
			}                 
		}
		public void Update(Kind item)
		{
			throw new NotImplementedException();
		}

		public void Update(IEnumerable<Kind> items)
		{
			throw new NotImplementedException();
		}

		public void Delete(Kind item)
		{            
			string str = System.Configuration.ConfigurationManager.ConnectionStrings["SkynetLibrary"].ConnectionString;
			System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(str);
			System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("delete from Kinds where Id=@Id", con);
			cmd.CommandType = System.Data.CommandType.Text;
			cmd.Parameters.AddWithValue("@Id", item.Id);
			try
			{
				con.Open();
				cmd.ExecuteNonQuery();                
			}
			catch (Exception ex)
			{
				//
			}
			finally
			{
				con.Close();
			}                 
		}

		public void Delete(IEnumerable<Kind> items)
		{
			
		}

		public Kind GetOne(object key)
		{
			Kind kind = null; 
			string str= System.Configuration.ConfigurationManager.ConnectionStrings["SkynetLibrary"].ConnectionString;
			System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(str);
			System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select * from Kinds where Id=@Id", con);
			cmd.CommandType = System.Data.CommandType.Text;
			cmd.Parameters.AddWithValue("@Id", key);
			try
			{
				con.Open();
				System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
				while (reader.Read())
				{
					kind = new Kind();
					kind.Id = (Guid)reader["Id"];
					kind.Description = reader["Description"] as string;
					kind.Title = reader["Title"] as string;
				}
				reader.Close();
			}
			catch (Exception)
			{

			}
			finally {
				con.Close();
			}
			return kind;
		}

		public IEnumerable<Kind> GetAll()
		{
			throw new NotImplementedException();
		}


        public Kind GetByTitle(string Title)
        {
            throw new NotImplementedException();
        }
    }
}
